<?php
class classifiedApi extends core
{
	public function init()
	{
		
	}

	public function releaseAction($arg)
	{
		base::init('member')->needLogin();
		$template	= $this->template();
	
		if (isPost())
		{
			
			if (empty($arg['classid']))
			{
				$template->assign('stat','请选择分类~');
				$template->display('refresh.php');
				echo "<script language=javascript>setTimeout(function(){window.history.go(-1);},2000);</script>";
				die;
			}
			
			include_once ROOT . '/libs/Zend/Session/Namespace.php';
			$codeSession	= new Zend_Session_Namespace('code');
			
			if (empty($arg['code']) || $arg['code'] !== $codeSession->code)
			{
				$template->assign('url','/app/?m=member&a=release');
				$template->assign('stat','验证码错误~');
				$template->display('refresh.php');
				die;
			}
			
			foreach ($arg as $key=>$val)
			{
				$arg[$key]	= strip_tags($val);
			}
			$arg['posttime']	= time();
			$arg['audit']		= 1;

			$id	= base::init('classified')->insert($arg);
			
			$page		= array(base::init('classified')->fetchRowById($id));
			$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
			
			$page		= array_shift(base::init('classified')->getTempList($page, $columns));

			$template	= $this->template();
			$template->assign('page', $page);
			$template->assign('columns', $columns);
			$template->display('do_index.php');
			//$this->refresh('发布成功，请等待管理员审核', '/app/?m=member&a=mylist');
		}
		else 
		{
			$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid'],'display'=>1));
			$columns	= base::init('column')->listcls($columns,0);
			$province	= base::init('province')->fetchAll();
			$infotype	= base::init('infotype')->fetchAll();
			
			$template->assign('userinfo',$_COOKIE);
			$template->assign('columns', $columns);
			$template->assign('province', $province);
			$template->assign('ip', get_real_ip());
			$template->assign('infotype', $infotype);
			$template->display('classified_release.php');
		}
	}
	
	//删除
	public function deleteAction($arg)
	{
		base::init('member')->needLogin();
		$template	= $this->template();
		$userinfo	= base::init('member')->getInfo();
		
		$id			= $arg['id'];
		$row		= base::init('classified')->fetchRowById($id);
		
		if ($row && $row['writer'] == $userinfo['username'])
		{
			base::init('classified')->delete("id=$id");
			$referrer	= '/app/?m=member&a=mylist';
			$stat		= '删除成功！';
		}
		else 
		{
			$referrer	= '/app/?m=member&a=mylist';
			$stat		= '没有权限！';
		}
		$template->assign('referrer',$referrer); 
		$template->assign('stat',$stat);
		$template->display('refresh.php');
	}
	
	//编辑
	public function editAction($arg)
	{
		base::init('member')->needLogin();
		$template	= $this->template();
		$userinfo	= base::init('member')->getInfo();
		
		$id			= $arg['id'];
		$row		= base::init('classified')->fetchRowById($id);
		
		if (!$row || $row['writer'] !== $userinfo['username'])
		{
			$referrer	= '/app/?m=member&a=mylist';
			$stat		= '没有权限！';
			$template->assign('referrer',$referrer); 
			$template->assign('stat',$stat);
			$template->display('refresh.php');
			die;
		}

		//提交
		if (isPost())
		{
			if (empty($arg['classid']))
			{
				$template->assign('stat','请选择分类~');
				$template->display('refresh.php');
				echo "<script language=javascript>setTimeout(function(){window.history.go(-1);},2000);</script>";
				die;
			}
			
			include_once ROOT . '/libs/Zend/Session/Namespace.php';
			$codeSession	= new Zend_Session_Namespace('code');
			
			if (empty($arg['code']) || $arg['code'] !== $codeSession->code)
			{
				$template->assign('url','/app/?m=member&a=release');
				$template->assign('stat','验证码错误~');
				$template->display('refresh.php');
				die;
			}
			$arg['flag']	= join(',',$arg['flag']);
			
			base::init('classified')->update($arg,"id=$id");
	
			$page		= array(base::init('classified')->fetchRowById($id));
			$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
			
			$page		= array_shift(base::init('classified')->getTempList($page, $columns));
			
			$template	= $this->template();
			$template->assign('attr', $this->attr);
			$template->assign('page', $page);
			$template->assign('columns', $columns);
			$template->display('do_index.php');		
			exit;
			//$template->assign('referrer','/app/?m=member&a=mylist'); 
			//$template->assign('stat','修改成功！');
			//$template->display('refresh.php');
			//die;
		}
		
		$classified	= base::init('classified')->fetchRowById($id);
		$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid'],'display'=>1));
		$province	= base::init('province')->fetchAll();
		$infotype	= base::init('infotype')->fetchAll();
		
		$template->assign('classified',$classified);
		$template->assign('userinfo',$_COOKIE);
		$template->assign('columns', $columns);
		$template->assign('province', $province);
		$template->assign('ip', get_real_ip());
		$template->assign('infotype', $infotype);
		$template->display('classified_edit.php');
	}

	public function addAction($arg)
	{
		if (isPost())
		{
			$arg['passdate']	= strtotime($arg['passdate']);
			$arg['posttime']	= time();
			$arg['audit']		= 1;
			$arg['flag']    	= isset($arg['flag']) ? join(',',$arg['flag']) : '';
			base::init('classified')->insert($arg);
			$this->refresh('添加成功', $arg['referrer']);
		}
		else 
		{
			$template	= $this->template();
			$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
			$columns	= base::init('column')->listcls($columns, 0);
			$infotype	= base::init('infotype')->fetchAll();
			
			$template->assign('columns', $columns);
			$template->assign('ip', get_real_ip());
			$template->assign('infotype', $infotype);
			$template->display('classified_add.php');
		}
	}
	
	public function previewAction($arg)
	{
		base::init('system')->make(array('page'),$arg['wid'],array($arg['classid']),array($arg['id']),'preview',0);
    	exit;
	}
}